import numpy as np
from sklearn.ensemble import IsolationForest
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# 生成模拟数据（正常交易和欺诈交易）
normal = np.random.normal(50, 15, 1000)  # 正常交易金额
fraud = np.random.normal(200, 50, 50)    # 欺诈交易金额
data = np.concatenate([normal, fraud]).reshape(-1,1)

# 使用K-means检测异常（需结合其他方法）
kmeans = KMeans(n_clusters=2, random_state=42)
clusters = kmeans.fit_predict(data)

# 可视化聚类结果
plt.scatter(data, np.zeros_like(data), c=clusters, cmap='coolwarm')
plt.title('Fraud Detection using K-means')
plt.xlabel('Transaction Amount')
plt.show()